package com.xcj.common.page;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/** 
 * <b>function:</b> 
 * @project base_frame 
 * @package com.xcj.common.page  
 * @fileName com.xcj.*
 * @createDate May 14, 2014 9:41:37 AM 
 * @author su_jian 
 */
public class PagerTag extends TagSupport {
	private static final long serialVersionUID = 5729832874890369508L;
	private String url;         //请求URI
	private int pageSize = 4;  //每页要显示的记录数
	private int currentPage = 1;     //当前页号
	private int totalsCount;    //总记录数
	private String formName;

	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		int pageCount = (totalsCount + pageSize - 1) / pageSize;  //计算总页数
		
		//拼写要输出到页面的HTML文本
		StringBuilder sb = new StringBuilder();
		sb.append("<style type=\"text/css\">");
		sb.append(".pagination {padding: 5px;float:right;font-size:12px;}");
		sb.append(".pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}");
		sb.append(".pagination a:hover, .pagination a:active {border: 1px solid #428bca;color: #000;text-decoration: none;}");
		sb.append(".pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #428bca;font-weight: bold;background-color: #428bca;color: #FFF;}");
		sb.append(".pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}");
		sb.append("</style>\r\n");
		sb.append("<div class=\"pagination\">\r\n");
		if(totalsCount == 0){
			sb.append("<strong>没有可显示的项目</strong>\r\n");
		}else{
			//页号越界处理
			if(currentPage > pageCount){		currentPage = pageCount;	}
			if(currentPage < 1){		currentPage = 1;	}
			
			sb.append("<form method=\"post\" action=\"").append(this.url);
			if(this.formName==null){
				sb.append("\" name=\"qPagerForm\">\r\n");
			}else{
				sb.append("\" name=\""+this.formName+"\">\r\n");
			}
			
			
			//获取请求中的所有参数
			HttpServletRequest request = (HttpServletRequest) pageContext
					.getRequest();
			Enumeration<String> enumeration = request.getParameterNames();
			String name = null;  //参数名
			String value = null; //参数值
			//把请求中的所有参数当作隐藏表单域
			while (enumeration.hasMoreElements()) {
				name =  enumeration.nextElement();
				value = request.getParameter(name);
				// 去除页号
				if (name.equals("currentPage")) {
					if (null != value && !"".equals(value)) {
						currentPage = Integer.parseInt(value);
					}
					continue;
				}
				sb.append("<input type=\"hidden\" name=\"")
				  .append(name)
				  .append("\" value=\"")
				  .append(value)
				  .append("\"/>\r\n");
			}
	
			// 把当前页号设置成请求参数
			sb.append("<input type=\"hidden\" name=\"").append("currentPage")
				.append("\" value=\"").append(currentPage).append("\"/>\r\n");
			
			// 输出统计数据
			sb.append("&nbsp;共<strong>").append(totalsCount)
				.append("</strong>项")
				.append(",<strong>")
				.append(pageCount)
				.append("</strong>页:&nbsp;\r\n");
			
			//上一页处理
			if (currentPage == 1) {
				sb.append("<span class=\"disabled\">&laquo;&nbsp;上一页")
					.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage(")
				  .append((currentPage - 1))
				  .append(")\">&laquo;&nbsp;上一页</a>\r\n");
			}
			
			//如果前面页数过多,显示"..."
			int start = 1; 
			if(this.currentPage > 4){
				start = this.currentPage - 1;
				sb.append("<a href=\"javascript:turnOverPage(1)\">1</a>\r\n");
				sb.append("<a href=\"javascript:turnOverPage(2)\">2</a>\r\n");
				sb.append("&hellip;\r\n");
			}
			//显示当前页附近的页
			int end = this.currentPage + 1;
			if(end > pageCount){
				end = pageCount;
			}
			for(int i = start; i <= end; i++){
				if(currentPage == i){   //当前页号不需要超链接
					sb.append("<span class=\"current\">")
						.append(i)
						.append("</span>\r\n");
				}else{
					sb.append("<a href=\"javascript:turnOverPage(")
						.append(i)
						.append(")\">")
						.append(i)
						.append("</a>\r\n");
				}
			}
			//如果后面页数过多,显示"..."
			if(end < pageCount - 2){
				sb.append("&hellip;\r\n");
			}
			if(end < pageCount - 1){
				sb.append("<a href=\"javascript:turnOverPage(")
				.append(pageCount - 1)
				.append(")\">")
				.append(pageCount - 1)
				.append("</a>\r\n");
			}
			if(end < pageCount){
				sb.append("<a href=\"javascript:turnOverPage(")
				.append(pageCount)
				.append(")\">")
				.append(pageCount)
				.append("</a>\r\n"); 
			}
			
			//下一页处理
			if (currentPage == pageCount) {
				sb.append("<span class=\"disabled\">下一页&nbsp;&raquo;")
					.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage(")
					.append((currentPage + 1))
					.append(")\">下一页&nbsp;&raquo;</a>\r\n");
			}
			sb.append("</form>\r\n");
	
			// 生成提交表单的JS
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("  function turnOverPage(no){\r\n");
			sb.append("    if(no>").append(pageCount).append("){");
			sb.append("      no=").append(pageCount).append(";}\r\n");
			sb.append("    if(no<1){no=1;}\r\n");
			
			if(this.formName==null){
				sb.append("    document.qPagerForm.currentPage.value=no;\r\n");
				sb.append("    document.qPagerForm.submit();\r\n");
				
			}else{
				sb.append("    document."+this.formName+".currentPage.value=no;\r\n");
				sb.append("    document."+this.formName+".submit();\r\n");
			}
			sb.append("  }\r\n");
			sb.append("</script>\r\n");
		}
		sb.append("</div>\r\n");
		
		//把生成的HTML输出到响应中
		try {
			pageContext.getOut().println(sb.toString());
		} catch (IOException e) {
			throw new JspException(e);
		}
		return SKIP_BODY;  //本标签主体为空,所以直接跳过主体
	}
	@SuppressWarnings("unchecked")
	public String doStartTagString(Page page,HttpServletRequest request) throws JspException {
		url = page.getUri();         //请求URI
		pageSize = page.getPageSize();  //每页要显示的记录数
		currentPage = page.getCurrentPage();     //当前页号
		totalsCount = page.getTotalsCount();    //总记录数
		formName = page.getFormName();	// 表单名称 
	
		int pageCount = (totalsCount + pageSize - 1) / pageSize;  //计算总页数
		
		//拼写要输出到页面的HTML文本
		StringBuilder sb = new StringBuilder();
		sb.append("<style type=\"text/css\">");
		sb.append(".pagination {padding: 5px;float:right;font-size:12px;}");
		sb.append(".pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}");
		sb.append(".pagination a:hover, .pagination a:active {border: 1px solid #428bca;color: #000;text-decoration: none;}");
		sb.append(".pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #428bca;font-weight: bold;background-color: #428bca;color: #FFF;}");
		sb.append(".pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}");
		sb.append("</style>\r\n");
		sb.append("<div class=\"pagination\">\r\n");
		if(totalsCount == 0){
			sb.append("<strong>没有可显示的项目</strong>\r\n");
		}else{
			//页号越界处理
			if(currentPage > pageCount){		currentPage = pageCount;	}
			if(currentPage < 1){		currentPage = 1;	}
			
			sb.append("<form method=\"post\" action=\"").append(this.url);
			if(this.formName==null){
				sb.append("\" name=\"qPagerForm\">\r\n");
			}else{
				sb.append("\" name=\""+this.formName+"\">\r\n");
			}
			
			
			//获取请求中的所有参数
			//HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			Enumeration<String> enumeration = request.getParameterNames();
			String name = null;  //参数名
			String value = null; //参数值
			//把请求中的所有参数当作隐藏表单域
			while (enumeration.hasMoreElements()) {
				name =  enumeration.nextElement();
				value = request.getParameter(name);
				// 去除页号
				if (name.equals("currentPage")) {
					if (null != value && !"".equals(value)) {
						currentPage = Integer.parseInt(value);
					}
					continue;
				}
				sb.append("<input type=\"hidden\" name=\"")
				.append(name)
				.append("\" value=\"")
				.append(value)
				.append("\"/>\r\n");
			}
			
			// 把当前页号设置成请求参数
			sb.append("<input type=\"hidden\" name=\"").append("currentPage")
			.append("\" value=\"").append(currentPage).append("\"/>\r\n");
			
			// 输出统计数据
			sb.append("&nbsp;共<strong>").append(totalsCount)
			.append("</strong>项")
			.append(",<strong>")
			.append(pageCount)
			.append("</strong>页:&nbsp;\r\n");
			
			//上一页处理
			if (currentPage == 1) {
				sb.append("<span class=\"disabled\">&laquo;&nbsp;上一页")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append((currentPage - 1))
				.append(")\">&laquo;&nbsp;上一页</a>\r\n");
			}
			
			//如果前面页数过多,显示"..."
			int start = 1; 
			if(this.currentPage > 4){
				start = this.currentPage - 1;
				sb.append("<a href=\"javascript:turnOverPage2(1)\">1</a>\r\n");
				sb.append("<a href=\"javascript:turnOverPage2(2)\">2</a>\r\n");
				sb.append("&hellip;\r\n");
			}
			//显示当前页附近的页
			int end = this.currentPage + 1;
			if(end > pageCount){
				end = pageCount;
			}
			for(int i = start; i <= end; i++){
				if(currentPage == i){   //当前页号不需要超链接
					sb.append("<span class=\"current\">")
					.append(i)
					.append("</span>\r\n");
				}else{
					sb.append("<a href=\"javascript:turnOverPage2(")
					.append(i)
					.append(")\">")
					.append(i)
					.append("</a>\r\n");
				}
			}
			//如果后面页数过多,显示"..."
			if(end < pageCount - 2){
				sb.append("&hellip;\r\n");
			}
			if(end < pageCount - 1){
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append(pageCount - 1)
				.append(")\">")
				.append(pageCount - 1)
				.append("</a>\r\n");
			}
			if(end < pageCount){
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append(pageCount)
				.append(")\">")
				.append(pageCount)
				.append("</a>\r\n"); 
			}
			
			//下一页处理
			if (currentPage == pageCount) {
				sb.append("<span class=\"disabled\">下一页&nbsp;&raquo;")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append((currentPage + 1))
				.append(")\">下一页&nbsp;&raquo;</a>\r\n");
			}
			sb.append("</form>\r\n");
			
			// 生成提交表单的JS
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("  function turnOverPage2(no){\r\n");
			sb.append("    if(no>").append(pageCount).append("){");
			sb.append("      no=").append(pageCount).append(";}\r\n");
			sb.append("    if(no<1){no=1;}\r\n");
			
			if(this.formName==null){
				sb.append("    document.qPagerForm.currentPage.value=no;\r\n");
				sb.append("    document.qPagerForm.submit();\r\n");
				
			}else{
				sb.append("    document."+this.formName+".currentPage.value=no;\r\n");
				sb.append("    document."+this.formName+".submit();\r\n");
			}
			sb.append("  }\r\n");
			sb.append("</script>\r\n");
		}
		sb.append("</div>\r\n");
		
		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	public String doStartTagStringWithoutJs(Page page,HttpServletRequest request) throws JspException {
		url = page.getUri();         //请求URI
		pageSize = page.getPageSize();  //每页要显示的记录数
		currentPage = page.getCurrentPage();     //当前页号
		totalsCount = page.getTotalsCount();    //总记录数
		formName = page.getFormName();	// 表单名称 
	
		int pageCount = (totalsCount + pageSize - 1) / pageSize;  //计算总页数
		
		//拼写要输出到页面的HTML文本
		StringBuilder sb = new StringBuilder();
		sb.append("<style type=\"text/css\">");
		sb.append(".pagination {padding: 5px;float:right;font-size:12px;}");
		sb.append(".pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}");
		sb.append(".pagination a:hover, .pagination a:active {border: 1px solid #428bca;color: #000;text-decoration: none;}");
		sb.append(".pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #428bca;font-weight: bold;background-color: #428bca;color: #FFF;}");
		sb.append(".pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}");
		sb.append("</style>\r\n");
		sb.append("<div class=\"pagination\" style=\"float:left;");
		if(totalsCount == 0){
			sb.append("margin-top: 30px;\">\r\n");
			sb.append("<strong>没有可显示的项目</strong>\r\n");
		}else{
			sb.append("\">\r\n");
			//页号越界处理
			if(currentPage > pageCount){		currentPage = pageCount;	}
			if(currentPage < 1){		currentPage = 1;	}
			
			sb.append("<form method=\"post\" action=\"").append(this.url);
			if(this.formName==null){
				sb.append("\" name=\"qPagerForm\">\r\n");
			}else{
				sb.append("\" name=\""+this.formName+"\">\r\n");
			}
			
			
			//获取请求中的所有参数
			//HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			Enumeration<String> enumeration = request.getParameterNames();
			String name = null;  //参数名
			String value = null; //参数值
			//把请求中的所有参数当作隐藏表单域
			while (enumeration.hasMoreElements()) {
				name =  enumeration.nextElement();
				value = request.getParameter(name);
				// 去除页号
				if (name.equals("currentPage")) {
					if (null != value && !"".equals(value)) {
						currentPage = Integer.parseInt(value);
					}
					continue;
				}
				sb.append("<input type=\"hidden\" name=\"")
				.append(name)
				.append("\" value=\"")
				.append(value)
				.append("\"/>\r\n");
			}
			
			// 把当前页号设置成请求参数
			sb.append("<input type=\"hidden\" name=\"").append("currentPage")
			.append("\" value=\"").append(currentPage).append("\"/>\r\n");
			
			// 输出统计数据
			sb.append("&nbsp;共<strong>").append(totalsCount)
			.append("</strong>项")
			.append(",<strong>")
			.append(pageCount)
			.append("</strong>页:&nbsp;\r\n");
			
			//上一页处理
			if (currentPage == 1) {
				sb.append("<span class=\"disabled\">&laquo;&nbsp;上一页")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append((currentPage - 1))
				.append(")\">&laquo;&nbsp;上一页</a>\r\n");
			}
			
			//如果前面页数过多,显示"..."
			int start = 1; 
			if(this.currentPage > 4){
				start = this.currentPage - 1;
				sb.append("<a href=\"javascript:turnOverPage2(1)\">1</a>\r\n");
				sb.append("<a href=\"javascript:turnOverPage2(2)\">2</a>\r\n");
				sb.append("&hellip;\r\n");
			}
			//显示当前页附近的页
			int end = this.currentPage + 1;
			if(end > pageCount){
				end = pageCount;
			}
			for(int i = start; i <= end; i++){
				if(currentPage == i){   //当前页号不需要超链接
					sb.append("<span class=\"current\">")
					.append(i)
					.append("</span>\r\n");
				}else{
					sb.append("<a href=\"javascript:turnOverPage2(")
					.append(i)
					.append(")\">")
					.append(i)
					.append("</a>\r\n");
				}
			}
			//如果后面页数过多,显示"..."
			if(end < pageCount - 2){
				sb.append("&hellip;\r\n");
			}
			if(end < pageCount - 1){
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append(pageCount - 1)
				.append(")\">")
				.append(pageCount - 1)
				.append("</a>\r\n");
			}
			if(end < pageCount){
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append(pageCount)
				.append(")\">")
				.append(pageCount)
				.append("</a>\r\n"); 
			}
			
			//下一页处理
			if (currentPage == pageCount) {
				sb.append("<span class=\"disabled\">下一页&nbsp;&raquo;")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage2(")
				.append((currentPage + 1))
				.append(")\">下一页&nbsp;&raquo;</a>\r\n");
			}
			sb.append("</form>\r\n");
			
			// 生成提交表单的JS
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("  function turnOverPage2(no){\r\n");
			sb.append("    if(no>").append(pageCount).append("){");
			sb.append("      no=").append(pageCount).append(";}\r\n");
			sb.append("    if(no<1){no=1;}\r\n");
			
			sb.append("if($(\".Js-hhj-domain-content-modal-current-uri\").val()==\"getCourseWaresPage\") {")
				.append("var tabVal = $(\".Js-hhj-domain-content-control-tab\").find(\"li[class='active']\").attr(\"data-val\");")
				.append("if(tabVal==\"course\") {$(\"form[name='allCoursePage']\").parent().find(\".currentPage\").text(no);")
				.append("getCoursePageAjaxAll(no,false,\"1\");}if(tabVal==\"tests\") {$(\"form[name='allTestsPage']\").parent().find(\".currentPage\").text(no);getCoursePageAjaxAll(no,false,\"2\");")
				.append("}}if($(\".Js-hhj-domain-content-modal-current-uri\").val()==\"getCoursePapersPage\") {getPaperPageAjaxAll(no,false);}");
			
			if(this.formName==null){
				sb.append("    document.qPagerForm.currentPage.value=no;\r\n");
				sb.append("    document.qPagerForm.submit();\r\n");
				
			}else{
				// $(document).find("form[name='"+this.formName+"']").parent().find("input[name='currentPage'].val(\""+currentPage+\"")")
				sb.append("    $(document).find(\"form[name='"+this.formName+"']\").parent().find(\"input[name='currentPage']\").val('"+currentPage+"');\r\n");
				sb.append("    \r\n");
			}
			sb.append("  }\r\n");
			sb.append("</script>\r\n");
			
		}
		sb.append("</div>\r\n");
		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	public String doStartTagTwoString(Page page,HttpServletRequest request) throws JspException {
		url = page.getUri();         //请求URI
		pageSize = page.getPageSize();  //每页要显示的记录数
		currentPage = page.getCurrentPage();     //当前页号
		totalsCount = page.getTotalsCount();    //总记录数
		formName = page.getFormName();	// 表单名称 
	
		int pageCount = (totalsCount + pageSize - 1) / pageSize;  //计算总页数
		
		//拼写要输出到页面的HTML文本
		StringBuilder sb = new StringBuilder();
		sb.append("<style type=\"text/css\">");
		sb.append(".pagination {padding: 5px;float:right;font-size:12px;}");
		sb.append(".pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}");
		sb.append(".pagination a:hover, .pagination a:active {border: 1px solid #428bca;color: #000;text-decoration: none;}");
		sb.append(".pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #428bca;font-weight: bold;background-color: #428bca;color: #FFF;}");
		sb.append(".pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}");
		sb.append("</style>\r\n");
		sb.append("<div class=\"pagination\">\r\n");
		if(totalsCount == 0){
			sb.append("<strong>没有可显示的项目</strong>\r\n");
		}else{
			//页号越界处理
			if(currentPage > pageCount){		currentPage = pageCount;	}
			if(currentPage < 1){		currentPage = 1;	}
			
			sb.append("<form method=\"post\" action=\"").append(this.url);
			if(this.formName==null){
				sb.append("\" name=\"qPagerForm\">\r\n");
			}else{
				sb.append("\" name=\""+this.formName+"\">\r\n");
			}
			
			
			//获取请求中的所有参数
			//HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			Enumeration<String> enumeration = request.getParameterNames();
			String name = null;  //参数名
			String value = null; //参数值
			//把请求中的所有参数当作隐藏表单域
			while (enumeration.hasMoreElements()) {
				name =  enumeration.nextElement();
				value = request.getParameter(name);
				// 去除页号
				if (name.equals("currentPage")) {
					if (null != value && !"".equals(value)) {
						currentPage = Integer.parseInt(value);
					}
					continue;
				}
				sb.append("<input type=\"hidden\" name=\"")
				.append(name)
				.append("\" value=\"")
				.append(value)
				.append("\"/>\r\n");
			}
			
			// 把当前页号设置成请求参数
			sb.append("<input type=\"hidden\" name=\"").append("currentPage")
			.append("\" value=\"").append(currentPage).append("\"/>\r\n");
			
			// 输出统计数据
			sb.append("&nbsp;共<strong>").append(totalsCount)
			.append("</strong>项")
			.append(",<strong>")
			.append(pageCount)
			.append("</strong>页:&nbsp;\r\n");
			
			//上一页处理
			if (currentPage <= 1) {
				sb.append("<span class=\"disabled\">&laquo;&nbsp;上一页")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append((currentPage - 1))
				.append(")\">&laquo;&nbsp;上一页</a>\r\n");
			}
			
			//如果前面页数过多,显示"..."
			int start = 1; 
			if(this.currentPage > 4){
				start = this.currentPage - 1;
				sb.append("<a href=\"javascript:turnOverPage4(1)\">1</a>\r\n");
				sb.append("<a href=\"javascript:turnOverPage4(2)\">2</a>\r\n");
				sb.append("&hellip;\r\n");
			}
			//显示当前页附近的页
			int end = this.currentPage + 1;
			if(end > pageCount){
				end = pageCount;
			}
			for(int i = start; i <= end; i++){
				if(currentPage == i){   //当前页号不需要超链接
					sb.append("<span class=\"current\">")
					.append(i)
					.append("</span>\r\n");
				}else{
					sb.append("<a href=\"javascript:turnOverPage4(")
					.append(i)
					.append(")\">")
					.append(i)
					.append("</a>\r\n");
				}
			}
			//如果后面页数过多,显示"..."
			if(end < pageCount - 2){
				sb.append("&hellip;\r\n");
			}
			if(end < pageCount - 1){
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append(pageCount - 1)
				.append(")\">")
				.append(pageCount - 1)
				.append("</a>\r\n");
			}
			if(end < pageCount){
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append(pageCount)
				.append(")\">")
				.append(pageCount)
				.append("</a>\r\n"); 
			}
			
			//下一页处理
			if (currentPage >= pageCount) {
				sb.append("<span class=\"disabled\">下一页&nbsp;&raquo;")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append((currentPage + 1))
				.append(")\">下一页&nbsp;&raquo;</a>\r\n");
			}
			sb.append("</form>\r\n");
			
			// 生成提交表单的JS
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("  function turnOverPage4(no){\r\n");
			sb.append("    if(no>").append(pageCount).append("){");
			sb.append("      no=").append(pageCount).append(";}\r\n");
			sb.append("    if(no<1){no=1;}\r\n");
			
			if(this.formName==null){
				sb.append("    document.qPagerForm.currentPage.value=no;\r\n");
				sb.append("    document.qPagerForm.submit();\r\n");
				
			}else{
				sb.append("    document."+this.formName+".currentPage.value=no;\r\n");
				sb.append("    document."+this.formName+".submit();\r\n");
			}
			sb.append("  }\r\n");
			sb.append("</script>\r\n");
		}
		sb.append("</div>\r\n");
		
		return sb.toString();
	}
	
	@SuppressWarnings("unchecked")
	public String doStartTagTwoStringWithoutJS(Page page,HttpServletRequest request) throws JspException {
		url = page.getUri();         //请求URI
		pageSize = page.getPageSize();  //每页要显示的记录数
		currentPage = page.getCurrentPage();     //当前页号
		totalsCount = page.getTotalsCount();    //总记录数
		formName = page.getFormName();	// 表单名称 
	
		int pageCount = (totalsCount + pageSize - 1) / pageSize;  //计算总页数
		
		//拼写要输出到页面的HTML文本
		StringBuilder sb = new StringBuilder();
		sb.append("<style type=\"text/css\">");
		sb.append(".pagination {padding: 5px;float:right;font-size:12px;}");
		sb.append(".pagination a, .pagination a:link, .pagination a:visited {padding:2px 5px;margin:2px;border:1px solid #aaaadd;text-decoration:none;color:#006699;}");
		sb.append(".pagination a:hover, .pagination a:active {border: 1px solid #428bca;color: #000;text-decoration: none;}");
		sb.append(".pagination span.current {padding: 2px 5px;margin: 2px;border: 1px solid #428bca;font-weight: bold;background-color: #428bca;color: #FFF;}");
		sb.append(".pagination span.disabled {padding: 2px 5px;margin: 2px;border: 1px solid #eee; color: #ddd;}");
		sb.append("</style>\r\n");
		sb.append("<div class=\"pagination\" style=\"float:left;");
		if(totalsCount == 0){
			sb.append("margin-top: 20px;\">\r\n");
			sb.append("<strong>没有可显示的项目</strong>\r\n");
		}else{
			sb.append("\">\r\n");
			//页号越界处理
			if(currentPage > pageCount){		currentPage = pageCount;	}
			if(currentPage < 1){		currentPage = 1;	}
			
			sb.append("<form method=\"post\" action=\"").append(this.url);
			if(this.formName==null){
				sb.append("\" name=\"qPagerForm\">\r\n");
			}else{
				sb.append("\" name=\""+this.formName+"\">\r\n");
			}
			
			
			//获取请求中的所有参数
			//HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			Enumeration<String> enumeration = request.getParameterNames();
			String name = null;  //参数名
			String value = null; //参数值
			//把请求中的所有参数当作隐藏表单域
			while (enumeration.hasMoreElements()) {
				name =  enumeration.nextElement();
				value = request.getParameter(name);
				// 去除页号
				if (name.equals("currentPage")) {
					if (null != value && !"".equals(value)) {
						currentPage = Integer.parseInt(value);
					}
					continue;
				}
				sb.append("<input type=\"hidden\" name=\"")
				.append(name)
				.append("\" value=\"")
				.append(value)
				.append("\"/>\r\n");
			}
			
			// 把当前页号设置成请求参数
			sb.append("<input type=\"hidden\" name=\"").append("currentPage")
			.append("\" value=\"").append(currentPage).append("\"/>\r\n");
			
			// 输出统计数据
			sb.append("&nbsp;共<strong>").append(totalsCount)
			.append("</strong>项")
			.append(",<strong>")
			.append(pageCount)
			.append("</strong>页:&nbsp;\r\n");
			
			//上一页处理
			if (currentPage <= 1) {
				sb.append("<span class=\"disabled\">&laquo;&nbsp;上一页")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append((currentPage - 1))
				.append(")\">&laquo;&nbsp;上一页</a>\r\n");
			}
			
			//如果前面页数过多,显示"..."
			int start = 1; 
			if(this.currentPage > 4){
				start = this.currentPage - 1;
				sb.append("<a href=\"javascript:turnOverPage4(1)\">1</a>\r\n");
				sb.append("<a href=\"javascript:turnOverPage4(2)\">2</a>\r\n");
				sb.append("&hellip;\r\n");
			}
			//显示当前页附近的页
			int end = this.currentPage + 1;
			if(end > pageCount){
				end = pageCount;
			}
			for(int i = start; i <= end; i++){
				if(currentPage == i){   //当前页号不需要超链接
					sb.append("<span class=\"current\">")
					.append(i)
					.append("</span>\r\n");
				}else{
					sb.append("<a href=\"javascript:turnOverPage4(")
					.append(i)
					.append(")\">")
					.append(i)
					.append("</a>\r\n");
				}
			}
			//如果后面页数过多,显示"..."
			if(end < pageCount - 2){
				sb.append("&hellip;\r\n");
			}
			if(end < pageCount - 1){
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append(pageCount - 1)
				.append(")\">")
				.append(pageCount - 1)
				.append("</a>\r\n");
			}
			if(end < pageCount){
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append(pageCount)
				.append(")\">")
				.append(pageCount)
				.append("</a>\r\n"); 
			}
			
			//下一页处理
			if (currentPage >= pageCount) {
				sb.append("<span class=\"disabled\">下一页&nbsp;&raquo;")
				.append("</span>\r\n");
			} else {
				sb.append("<a href=\"javascript:turnOverPage4(")
				.append((currentPage + 1))
				.append(")\">下一页&nbsp;&raquo;</a>\r\n");
			}
			sb.append("</form>\r\n");
			
			// 生成提交表单的JS
			sb.append("<script language=\"javascript\">\r\n");
			sb.append("  function turnOverPage4(no){\r\n");
			sb.append("    if(no>").append(pageCount).append("){");
			sb.append("      no=").append(pageCount).append(";}\r\n");
			sb.append("    if(no<1){no=1;}\r\n");
			
			sb.append("if($(\".Js-hhj-domain-content-modal-current-selected-uri\").val()==\"getCourseWaresSelectedPage\") {")
				.append("var tabVal = $(\".Js-hhj-domain-content-control-tab\").find(\"li[class='active']\").attr(\"data-val\");")
				.append("}if(tabVal==\"course\") {getCoursePageAjaxSelected(no,false,\"1\");")
				.append("}if(tabVal==\"tests\") {getCoursePageAjaxSelected(no,false,\"2\");")
	    		.append("}");
			
			if(this.formName==null){
				sb.append("    document.qPagerForm.currentPage.value=no;\r\n");
				sb.append("    document.qPagerForm.submit();\r\n");
				
			}else{
				// $(document).find("form[name='"+this.formName+"']").parent().find("input[name='currentPage'].val(\""+currentPage+\"")")
				sb.append("    $(document).find(\"form[name='"+this.formName+"']\").parent().find(\"input[name='currentPage']\").val('"+currentPage+"');\r\n");
				sb.append("   \r\n");
			}
			sb.append(" }\r\n");
			sb.append("</script>\r\n");
		}
		sb.append("</div>\r\n");
		
		return sb.toString();
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * @return the totalsCount
	 */
	public int getTotalsCount() {
		return totalsCount;
	}

	/**
	 * @param totalsCount the totalsCount to set
	 */
	public void setTotalsCount(int totalsCount) {
		this.totalsCount = totalsCount;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}
	
}
